package com.huazhi.wzsnw.service.impl;

import com.huazhi.wzsnw.mapper.AlertValueMapper;
import com.huazhi.wzsnw.model.AlertValue;
import com.huazhi.wzsnw.service.AlertValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class AlertValueServiceImpl implements AlertValueService {

    @Autowired
    private AlertValueMapper alertValueMapper;


    @Override
    public int save(AlertValue record) {
        return alertValueMapper.insertSelective(record);
    }

    @Override
    public int delete(AlertValue record) {
        return alertValueMapper.deleteByPrimaryKey(record.getTid());
    }

    @Override
    public int deletes(List<AlertValue> records) {
        for (AlertValue alertValue:records){
            delete(alertValue);
        }
        return 0;
    }

    @Override
    public int update(AlertValue record) {
        return alertValueMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public AlertValue findByTid(String tid) {
        return alertValueMapper.selectByPrimaryKey(tid);
    }

    @Override
    public List<AlertValue> findPagingByListTid(List<String> stringList, Integer pageIndex, Integer pageSize) {
        return alertValueMapper.findPagingByListTid(stringList, pageIndex, pageSize);
    }

    @Override
    public List<String> getSearchAlertValueList(String search) {
        return alertValueMapper.getSearchAlertValueList(search);
    }

    @Override
    public List<String> getSearchAlertValueListByRequest(HttpServletRequest request) {
        String search = request.getParameter("key");
        String check = request.getParameter("selectdata");
        List<String> list = new ArrayList<>();
        if(check != null && !"".equals(check)){
            String [] ids = search.split(",");
            list = Arrays.asList(ids);
        }else {
            List<String> params = new ArrayList<String>();
            if (search == null) { search = ""; }
            if ("".equals(search)) {
                params.add("");
            } else if (search.contains(" ")) {
                params = Arrays.asList(search.split(" "));
            } else if (!search.contains(" ")) {
                params.add(search);
            }
            boolean firstSearch = true;
            for (String oneParam : params) {
                if (firstSearch) {
                    list = getSearchAlertValueList(oneParam);
                    firstSearch = false;
                } else {
                    List<String> middleList = getSearchAlertValueList(oneParam);
                    list.retainAll(middleList);
                }
            }
        }
        return list;
    }

    @Override
    public AlertValue selectAlertValueByField(String field, String value) {
        return alertValueMapper.selectAlertValueByField(field, value);
    }

    @Override
    public void updateAlertValueState(AlertValue alertValue) {
        switch (alertValue.getTid()){
            case "58cc22fd-0195-47a5-adde-7da96908935f":AlertValue.setFanxibengRun(Integer.parseInt(alertValue.getValue()));break;
            case "bf42a38d-a68f-42f8-b745-7b1a6658a80d":AlertValue.setChanshuibengRun(Integer.parseInt(alertValue.getValue()));break;
            case "22eda0dd-c09f-4e14-adb6-a2f158e9b60c":AlertValue.setGufengji1Run(Integer.parseInt(alertValue.getValue()));break;
            case "435e93ec-5b44-4a45-bb55-230c0a87754d":AlertValue.setGufengji2Run(Integer.parseInt(alertValue.getValue()));break;
            case "f0de07b4-f326-40e3-b9a9-eacf1f6e4446":AlertValue.setHuiliubengRun(Integer.parseInt(alertValue.getValue()));break;
            case "1803c4da-dced-4d0f-b50c-d12b1e36d4de":AlertValue.setJiaobanjiRun(Integer.parseInt(alertValue.getValue()));break;
            case "5100e41e-be3b-438c-9cb5-f355fc66eeda":AlertValue.setJinshuibengRun(Integer.parseInt(alertValue.getValue()));break;
            case "855c9fa4-a08a-43bc-bc63-204f1539d7d6":AlertValue.setFanxibengStop(Integer.parseInt(alertValue.getValue()));break;
            case "e99e74ed-8c41-45f9-a3f2-ac7f4579ffd1":AlertValue.setChanshuibengStop(Integer.parseInt(alertValue.getValue()));break;
            case "ecd32633-7bf3-411d-8e15-89ce8f8a59f7":AlertValue.setGufengji1Stop(Integer.parseInt(alertValue.getValue()));break;
            case "dd4f8489-a583-4a3e-9859-23ada85cfdd5":AlertValue.setGufengji2Stop(Integer.parseInt(alertValue.getValue()));break;
            case "d81f3930-8d4a-40f4-9b66-1c9acc96edfd":AlertValue.setHuiliubengStop(Integer.parseInt(alertValue.getValue()));break;
            case "db1d228b-ef6b-4cd9-b638-b259f888edce":AlertValue.setJiaobanjiStop(Integer.parseInt(alertValue.getValue()));break;
            case "73bfd70f-bf96-428d-be1a-bdc68edadfa5":AlertValue.setJinshuibengStop(Integer.parseInt(alertValue.getValue()));break;
            case "d98f62f7-eada-45be-9ea2-6e0257e690f3":AlertValue.setUpdatetime(Integer.parseInt(alertValue.getValue()));break;
            default:break;
        }
    }
}
